#include <iostream>
#include <vector>
#include <map>
using namespace std;

class Solution
{
public:
    long long maximumTotalDamage(vector<int> &power)
    {
        map<int, long long> mp;
        for (int p : power)
        {
            mp[p] += p;
        }
        long long maxV = 0;
        int i0 = -1, i1 = -1, i2 = -1;
        for (auto it = mp.begin(); it != mp.end(); it++)
        {
            if (i2 == -1)
            {
            }
            else if (i1 == -1)
            {
                // use it
                long long damage_use = it->second;
                if (it->first - i2 <= 2)
                {
                }
                else
                {
                    damage_use += mp[i2];
                }
                // unuse it
                long long damage_unuse = mp[i2];
                it->second = std::max(damage_use, damage_unuse);
            }
            else if (i0 == -1)
            {
                // use it
                long long damage_use = it->second;
                if (it->first - i2 <= 2)
                {
                    if (it->first - i1 <= 2)
                    {
                        
                    }
                    else
                    {
                        damage_use += mp[i1];
                    }
                }
                else
                {
                    damage_use += mp[i2];
                }
                // unuse it
                long long damage_unuse = mp[i2];
                it->second = std::max(damage_use, damage_unuse);
            }
            else
            {
                // use it
                long long damage_use = it->second;
                if (it->first - i2 <= 2)
                {
                    if (it->first - i1 <= 2)
                    {
                        damage_use += mp[i0];
                    }
                    else
                    {
                        damage_use += mp[i1];
                    }
                }
                else
                {
                    damage_use += mp[i2];
                }
                // unuse it
                long long damage_unuse = mp[i2];
                it->second = std::max(damage_use, damage_unuse);
            }
            maxV = std::max(maxV, it->second);
            i0 = i1;
            i1 = i2;
            i2 = it->first;
        }
        return maxV;
    }
};
int main()
{
    cout << "Code3186" << endl;
    Solution s;
    vector<int> power = {1, 1, 3, 4};
    std::cout << s.maximumTotalDamage(power);
    return 0;
}